package com.atguigu.ggkt.vod.controller;

import com.atguigu.ggkt.exception.CustomException;
import com.atguigu.ggkt.result.Result;
import com.atguigu.ggkt.vod.service.VodService;
import com.atguigu.ggkt.vod.utils.ConstantPropertiesUtil;
import com.atguigu.ggkt.vod.utils.Signature;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Random;

@Api(tags = "腾讯云点播")
@RestController
@RequestMapping("/admin/vod")
//@CrossOrigin
public class VodController {
    @Resource
    private VodService vodService;

    @ApiOperation("返回Web客户端上传视频签名")
    @GetMapping("sign")
    public Result sign(){
        Signature sign = new Signature();
        // 设置 App 的云 API 密钥
        sign.setSecretId(ConstantPropertiesUtil.ACCESS_KEY_ID);
        sign.setSecretKey(ConstantPropertiesUtil.ACCESS_KEY_SECRET);
        sign.setCurrentTime(System.currentTimeMillis() / 1000);
        sign.setRandom(new Random().nextInt(java.lang.Integer.MAX_VALUE));
        sign.setSignValidDuration(3600 * 24 * 2); // 签名有效期：2天
        try {
            String signature = sign.getUploadSignature();
//            System.out.println("signature : " + signature);
            return Result.ok(signature);
        } catch (Exception e) {
//            System.out.print("获取签名失败");
            e.printStackTrace();
            throw new CustomException(20001, "获取签名失败");
        }
    }

    //上传视频接口
    /*
    //服务端JavaSDK上传的媒体文件路径是不支持URL的，目前仅支持本地路径。目前腾讯云点播只服务端SDK只支持本地视频上传，不支持文件流上传，很垃圾，而阿里云支持流式上传
    @ApiOperation("上传视频接口")
    @PostMapping("upload")
    public Result upload(@ApiParam(name = "file", value = "文件", required = true)
                         @RequestPart("file") MultipartFile file) throws IOException {
        InputStream inputStream = file.getInputStream();
        String originalFilename = file.getOriginalFilename();
        String fileId = vodService.uploadVideo(inputStream, originalFilename);
        return Result.ok(fileId);
    }
     */

    //删除腾讯云视频
    @ApiOperation("腾讯云视频删除接口")
    @DeleteMapping("remove/{fileId}")
    public Result remove(@PathVariable String fileId){
        vodService.removeVideo(fileId);
        return Result.ok(null);
    }

}
